package com.edu.realtime.app.dwd.db;

import com.edu.realtime.util.MyKafkaUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @author zcx
 * @create 2022-10-19 22:40
 * 交易域：订单预处理
 */
public class DwdTradeOrderPreProcess {
    public static void main(String[] args) {
        //TODO 1.基本环境准备
        //1.1 指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.2 设置并行度
        env.setParallelism(4);
        //1.3 指定表执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        //1.4 设置状态的失效时间
        tableEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(15*60 + 10));

        /*// TODO 2. 检查点相关设置
        // 2.1 开启检查点
        env.enableCheckpointing(5000L, CheckpointingMode.EXACTLY_ONCE);
        // 2.2 设置检查点超时时间
        env.getCheckpointConfig().setCheckpointTimeout(60000L);
        // 2.3 设置两个检查点之间最小时间间隔
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(2000L);
        // 2.4 设置job取消后检查点是否保留
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.DELETE_ON_CANCELLATION);
        // 2.5 设置重启策略
        env.setRestartStrategy(RestartStrategies.failureRateRestart(3, Time.days(30), Time.seconds(3)));
        // 2.6 设置状态后端
        env.setStateBackend(new HashMapStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://nameservice1:8020/gmall/ck");
        // 2.7 设置操作hadoop的用户
        System.setProperty("HADOOP_USER_NAME", "root");*/
        //TODO 3.从kafka的topic_db主题中读取数据 创建动态表
        tableEnv.executeSql(MyKafkaUtil.getTopicDbDDL("dwd_trade_order_pre_group"));
        //TODO 4.过滤出订单明细
        Table orderDetail = tableEnv.sqlQuery("select\n" +
                "data['id'] id,\n" +
                "data['order_id'] order_id,\n" +
                "data['course_id'] course_id,\n" +
                "data['course_name'] course_name,\n" +
                "data['create_time'] create_time,\n" +
                "data['final_amount'] final_amount,\n" +
                "ts od_ts\n" +
                "from topic_db\n" +
                "where `table`='order_detail'\n" +
                "and `type` = 'insert'");
        tableEnv.createTemporaryView("order_detail", orderDetail);



        //TODO 5.过滤出订单
        Table orderInfo = tableEnv.sqlQuery("select\n" +
                "data['id'] id,\n" +
                "data['user_id'] user_id,\n" +
                "data['province_id'] province_id,\n" +
                "data['update_time'] update_time,\n" +
                "data['order_status'] order_status,\n" +
                "data['session_id'] session_id,\n" +
                "`type`,\n" +
                "`old`,\n" +
                "ts oi_ts\n" +
                "from topic_db\n" +
                "where `table`='order_info'\n" +
                "and (`type` = 'insert' or `type` = 'update')");
        tableEnv.createTemporaryView("order_info", orderInfo);



        // TODO 5. 从dwd_traffic_page_log中读取数据,创建动态表
        tableEnv.executeSql(MyKafkaUtil.getTopicPageLogDDL("dwd_order_page_log_group"));

        Table pageTable = tableEnv.sqlQuery("select\n" +
                "common['sid'] sid,\n" +
                "common['sc'] sc\n" +
                "from\n" +
                "dwd_traffic_page_log\n");
        tableEnv.createTemporaryView("page_table", pageTable);



        //TODO 6.将三张表关联
        Table resultTable = tableEnv.sqlQuery("select\n" +
                "od.id,\n" +
                "od.order_id,\n" +
                "od.course_id,\n" +
                "od.course_name,\n" +
                "date_format(od.create_time, 'yyyy-MM-dd') date_id,\n" +
                "od.create_time,\n" +
                "od.final_amount,\n" +
                "od.od_ts,\n" +
                "oi.user_id,\n" +
                "oi.province_id,\n" +
                "date_format(oi.update_time, 'yyyy-MM-dd') operate_date_id,\n" +
                "oi.update_time,\n" +
                "oi.order_status,\n" +
                "tpl.sc source_id,\n" +
                "oi.`type`,\n" +
                "oi.`old`,\n" +
                "oi.oi_ts,\n" +
                "current_row_timestamp() row_op_ts\n" +
                "from order_detail od\n" +
                "join order_info oi\n" +
                "on od.order_id = oi.id\n" +
                "join page_table tpl\n" +
                "on oi.session_id = tpl.sid");
        tableEnv.createTemporaryView("result_table", resultTable);



        //TODO 7.创建动态表
        tableEnv.executeSql("create table dwd_trade_order_pre_process(\n" +
                "id string,\n" +
                "order_id string,\n" +
                "course_id string,\n" +
                "course_name string,\n" +
                "date_id string,\n" +
                "create_time string,\n" +
                "final_amount string,\n" +
                "od_ts string,\n" +
                "user_id string,\n" +
                "province_id string,\n" +
                "operate_date_id string,\n" +
                "update_time string,\n" +
                "order_status string,\n" +
                "source_id string,\n" +
                "`type` string,\n" +
                "`old` map<string,string>,\n" +
                "oi_ts string,\n" +
                "row_op_ts timestamp_ltz(3),\n" +
                "primary key(id) not enforced)"+ MyKafkaUtil.getUpsertKafkaDDL("dwd_trade_order_pre_process"));

        //TODO 8.写入
        tableEnv.executeSql("insert into dwd_trade_order_pre_process select * from result_table");
    }
}
